package cn.taobaoke.restful.controller;

import cn.taobaoke.restful.domain.UserEntity;
import cn.taobaoke.restful.service.UserService;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * UserController类.
 * +-----------------------------------------------------------------------------------------------+
 * |类名称: UserController                                                                         |
 * |创建时间: 3/16/17                                                                               |
 * |功能简介:                                                                                       |
 * |参考链接:                                                                                       |
 * +-----------------------------------------------------------------------------------------------+
 *
 * @author luch
 */
@RestController
@RequestMapping("/{domain}/{domainId}/api/users")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserController extends BaseController {

    // 1. search users by page

    /**
     * 查询会员接口
     *
     * @param page
     * @param size
     * @param agentId
     * @return
     */
    @GetMapping("")
    public Result search(@PathVariable Long domainId, String keyword, Integer page, Integer size, Long agentId) {
        logger.debug("GET /{}/api/users?page={}&size={}&agent={}", domainId, page, size, agentId);

        Pageable pageable = createPageable(page, size);
        Page<UserEntity> pagingUsers = userService.search(keyword, agentId, pageable, domainId);
        return wrapPage(pageable, pagingUsers);
    }

    private final @NonNull
    UserService userService;

    private static Logger logger = LogManager.getLogger(UserController.class);
}
